using System;
using System.Collections;
using System.Collections.Generic;
//using System.Random;
using System.Text;
//using System.Collections.ArrayList;
using MULENetworking;
using System.Drawing;

namespace MULEServer
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class Map : IMap
    {
        /** Mountain placement:
         *  This function determines where the mountains
         *  are placed, and also determines how many
         *  mountains should be placed on one plot.  All
         *  of the mountain placement will be done randomly.
         *  param occupied: a list of plots that mountains
         *  cannot be placed on.
         *  param horizontal: the horizontal size of the map.
         *  param vertical: the vertical size of the map.
         *  param number: number of squares to contain mountains.
         */

        private MapSquare[,] map;

        public MapSquare[,] Mapp
        {
            get { return map; }
        }
        private string mapName;


        public string Text
        {
            get { return mapName; }
            set { mapName = value; }
        }

        private int horizontal, vertical;
        /// <summary>
        /// 
        /// </summary>
        public Map()
        {
            map = new MapSquare[9,5];

            horizontal = 9;
            vertical = 5;

            
            map[4, 0] = new MapSquare(new Point(400, 0), "food", 0, "empty");       
            map[4, 1] = new MapSquare(new Point(400, 100), "food", 0, "empty");            
            map[4, 3] = new MapSquare(new Point(400, 300), "food", 0, "empty");
            map[4, 4] = new MapSquare(new Point(400, 400), "food", 0, "empty");

            MapSquare store = new MapSquare(new Point(400, 200), "store", 0, "empty");
            map[4,2] = store;
            MountainPlacement(20);
            for (int x = 0; x < 9; x++)
            {
                for (int y = 0; y < 5; y++)
                {
                    if (map[x,y] == null)
                        map[x,y] = new MapSquare(new Point(x * 100, y * 100), "energy", 0, "empty");
                }
            }



        }
        

        private void MountainPlacement(int numMountains)
        {
            int x = 0;
            int y = 0;
            //Boolean contains;
            //Point tempLoc;
            //MapSquare mapSquare, MStemp;
            Random rand = new Random();

            //NOTICE: this makes no sense what so ever....
            //squares = occupied;
            int totMountains = 0;
            int mountainsToPlace;
            while(totMountains < numMountains)
            {
                x = rand.Next(0, horizontal);
                y = rand.Next(0, vertical);
                //Boolean contains = false;

                if (map[x,y] == null)
                {
                    mountainsToPlace = rand.Next(1, 4);
                    totMountains += mountainsToPlace;
                    if (totMountains > numMountains)
                    {
                        mountainsToPlace = totMountains - numMountains;
                    }
                    map[x,y] = new MapSquare(new Point(x * 100, y * 100), "smithore", mountainsToPlace, "empty");
                }
            }
        }

        public void Chooseplot(int x, int y, IPlayer owner)
        {
            map[x, y].Owner = owner;
        }


        public void Draw(Graphics g) //again...WTF was the override for?? extend a method or something first...eh...
        {
            foreach(MapSquare i in map)
                    i.Draw(g);
                
        }




        #region IMap Members


        public void InstallMule(int x, int y, string mule)
        {
            map[x, y].Mule = mule;
        }

        #endregion
    }
}
